Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

320
Views
django | display the name value from select option with ajax (jquery) not the id

this is the ajax code

    $('.addtasksubmit').click(function(e) {
        e.preventDefault();
        

        $.ajax({
            url: 'addtodo',
            type: "POST",
            data: {
                title: $('#task-title').val(),
                category: $('#catsel option:selected').val(),
                'csrfmiddlewaretoken':$('input[name=csrfmiddlewaretoken]').val()
            },
            success: function(response) {
                console.log(response)
                $('.todos-box').append(`
                  <a href="#" class="category-link">${response.category}</a>
                `)
                $("#addtodo").trigger('reset');
                $("#exampleModal").modal('hide');
            }
        })
    })

So ${response.category} gives me the id of the category but i need the category name to be viewed

and this is views.py

        response = {
        'title': request.POST['title'],
        'category' : request.POST['category'],
        }
        todo = Todo.objects.create(title=response['title'],
                                     category=Category.objects.get(id=response['category']),
                                     user=request.user)   
        todo.save()
   return JsonResponse(response, safe=False)

this is HTML

<select name="category" class="form-select form-select-sm" id="catsel">
            <option seleted>Choose Category</option>
            {% for category in categories %}
            <option value="{{ category.id }}">{{ category }}</option>
            {% endfor %}
</select>

is there any way to view the category name with ajax not the id? or how can i get the name of the category with its id.

sorry for my bad english..

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

so i have solved this problem after 5 days of trying.

added this to the views

        response['category']=Category.objects.filter(id=category)
                           .values('name').first()
        data = json.dumps(response)
        return HttpResponse(data)

and ajax

                <div class="category-list flex-1">
                  <a href="#" class="category-link">${response['category'].name}</a>
                </div> 

and also added dataType: 'json'

over 4 years ago · Santiago Trujillo Report

0

It would be helpful to get the HTML select input with the categories from the template. Based on what you posted please adjust the views.py to

  todo = Todo.objects.create(title=response['title'],
  category = Category.objects.get(id=response['category']),
                                 user=request.user)   
  todo.save()  

  response = {
      'title': request.POST['title'],
      'category' : category.name,
  }


return JsonResponse(response, safe=False)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!